<template>
  <div>
    <el-divider />
    <el-row>
      <el-col :span="12">
        <el-form-item prop="transferTaskList">
          <el-checkbox-group v-model="form.transferTaskList">
            <el-checkbox label="TABLE_TRANSFER">表迁移</el-checkbox>
            <!-- <el-checkbox label="DATA_TRANSFER" :disabled="!form.transferTaskList.includes('TABLE_TRANSFER')">数据迁移</el-checkbox>
            <el-checkbox label="SCRIPT_TRANSFER">脚本迁移</el-checkbox> -->
          </el-checkbox-group>
        </el-form-item>
      </el-col>
    </el-row>
    <!-- 条件查询 -->
    <div class="head-container">
      <div>
        <!-- 搜索 -->
        <el-input
          v-model="query.sourceFileName"
          clearable
          size="small"
          placeholder="请输入脚本名称搜索"
          style="width: 200px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <el-select
          v-model="query.status"
          clearable
          size="small"
          placeholder="分析状态"
          class="filter-item"
          style="width: 120px"
          @change="crud.toQuery"
        >
          <el-option v-for="item in statusList" :key="item.key" :label="item.label" :value="item.key" />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
    </div>
    <!-- 转换完成列表 -->
    <el-table
      ref="table"
      :key="itemKey"
      v-loading="crud.loading"
      border
      :data="crud.data"
      max-height="300"
      @filter-change="crud.handleFilterChange"
      @select="crud.selectChange"
      @select-all="crud.selectAllChange"
      @selection-change="crud.selectionChangeHandler"
    >
      <el-table-column :show-overflow-tooltip="true" label="原文件名称" prop="sourceFileName" />
      <el-table-column :show-overflow-tooltip="true" label="分析状态" prop="status" width="80">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 'TODO'" type="info">待分析</el-tag>
          <el-tag v-if="scope.row.status === 'PROCESSING'">分析中</el-tag>
          <el-tag v-if="scope.row.status === 'SUCCESS'" type="success">成功</el-tag>
          <el-tag v-if="scope.row.status === 'FAIL'" type="danger">失败</el-tag>
        </template>
      </el-table-column>
      <el-table-column :show-overflow-tooltip="true" label="脚本大小" prop="sourceFileSize" width="80" />
      <el-table-column prop="sourceTableNameList" label="源表">
        <template slot-scope="scope">
          <el-tag
            v-for="sourceTableName in scope.row.sourceTableNameList"
            :key="sourceTableName"
            closable
            :disable-transitions="false"
            @close="sourceTableNamehandleClose(scope.row, sourceTableName)"
          >
            {{ sourceTableName }}
          </el-tag>
          <el-input
            v-if="scope.row.newSourceTableNameVisible"
            ref="addSourceTableName"
            v-model="newSourceTableName"
            class="input-new-tag"
            size="small"
            @keyup.enter.native="handleSourceTableInputConfirm(scope.row)"
            @blur="handleSourceTableInputConfirm(scope.row)"
          />
          <el-button v-else class="button-new-tag" size="small" :disabled="scope.row.status !== 'SUCCESS'" @click="showSourceTableInput(scope.row)">+ Add Table</el-button>
        </template>
      </el-table-column>
      <el-table-column prop="targetTableNames" label="目标表">
        <template slot-scope="scope">
          <el-tag
            v-for="targetTableName in scope.row.targetTableNameList"
            :key="targetTableName"
            closable
            :disable-transitions="false"
            @close="targetTableNamehandleClose(scope.row, targetTableName)"
          >
            {{ targetTableName }}
          </el-tag>
          <el-input
            v-if="scope.row.newTargetTableNameVisible"
            ref="addTargetTableName"
            v-model="newTargetTableName"
            class="input-new-tag"
            size="small"
            @keyup.enter.native="handleTargetTableInputConfirm(scope.row)"
            @blur="handleTargetTableInputConfirm(scope.row)"
          />
          <el-button v-else class="button-new-tag" size="small" :disabled="scope.row.status !== 'SUCCESS'" @click="showTargetTableInput(scope.row)">+ Add Table</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页组件-->
    <pagination />

  </div>
</template>
<script>
import CRUD, { crud, header, presenter } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import pagination from '@crud/Pagination'
import scriptAnalyzeListApi from '@/api/script/analyze/script-analyze-list'
import scriptTableConsanguinityApi from '@/api/script/common/scriptTableConsanguinity'

const defaultCrud = CRUD({ title: '已转换清单', url: '/api/script/analyze/list/queryConsanguinityList', crudMethod: { ...scriptAnalyzeListApi }})

export default {
  name: 'AnalyzeFinishResult',
  components: { rrOperation, pagination },
  mixins: [presenter(defaultCrud), header(), crud()],
  model: {
    prop: 'form',
    event: 'changes'
  },
  props: {
    form: {
      required: true,
      type: Object
    }
  },
  data() {
    return {
      itemKey: 0,
      newSourceTableName: null,
      newTargetTableName: null,
      statusList: [
        { key: 'SUCCESS', label: '成功' },
        { key: 'FAIL', label: '失败' },
        { key: 'PROCESSING', label: '分析中' }
      ],
      indexArray: [],
      tableData: []
    }
  },
  created() {
  },
  methods: {
    [CRUD.HOOK.beforeRefresh](crud) {
      crud.query.analyzeTaskPkId = this.form.pkId
    },
    [CRUD.HOOK.afterRefresh](crud) {
      crud.data.forEach(item => {
        item.newSourceTableNameVisible = false
        item.newTargetTableNameVisible = false
      })
    },
    sourceTableNamehandleClose(row, sourceTableName) {
      const dataForm = {
        taskPkId: row.analyzeTaskPkId,
        scriptListPkId: row.pkId,
        scriptName: row.sourceFileName,
        tableName: sourceTableName,
        tableType: 'SOURCE',
        dataFrom: 'SCRIPT_ANALYZE'
      }
      scriptTableConsanguinityApi.del(dataForm).then(res => {
        this.crud.notify('删除成功！', CRUD.NOTIFICATION_TYPE.SUCCESS)
        this.crud.refresh()
      })
    },
    targetTableNamehandleClose(row, targetTableName) {
      const dataForm = {
        taskPkId: row.analyzeTaskPkId,
        scriptListPkId: row.pkId,
        scriptName: row.sourceFileName,
        tableName: targetTableName,
        tableType: 'TARGET',
        dataFrom: 'SCRIPT_ANALYZE'
      }
      scriptTableConsanguinityApi.del(dataForm).then(res => {
        this.crud.notify('删除成功！', CRUD.NOTIFICATION_TYPE.SUCCESS)
        this.crud.refresh()
      })
    },
    showSourceTableInput(row) {
      row.newSourceTableNameVisible = true
      this.itemKey = Math.random()
    },
    showTargetTableInput(row) {
      row.newTargetTableNameVisible = true
      this.itemKey = Math.random()
    },
    handleSourceTableInputConfirm(row) {
      if (this.newSourceTableName === null || this.newSourceTableName.trim() === '') {
        this.newSourceTableName = null
        this.crud.refresh()
        return
      }
      const dataForm = {
        taskPkId: row.analyzeTaskPkId,
        scriptListPkId: row.pkId,
        scriptName: row.sourceFileName,
        tableName: this.newSourceTableName,
        tableType: 'SOURCE',
        dataFrom: 'SCRIPT_ANALYZE'
      }
      scriptTableConsanguinityApi.add(dataForm).then(res => {
        this.crud.notify('新增成功！', CRUD.NOTIFICATION_TYPE.SUCCESS)
        this.newSourceTableName = null
        this.crud.refresh()
      })
    },
    handleTargetTableInputConfirm(row) {
      if (this.newTargetTableName === null || this.newTargetTableName.trim() === '') {
        this.newTargetTableName = null
        this.crud.refresh()
        return
      }
      const dataForm = {
        taskPkId: row.analyzeTaskPkId,
        scriptListPkId: row.pkId,
        scriptName: row.sourceFileName,
        tableName: this.newTargetTableName,
        tableType: 'TARGET',
        dataFrom: 'SCRIPT_ANALYZE'
      }
      scriptTableConsanguinityApi.add(dataForm).then(res => {
        this.crud.notify('新增成功！', CRUD.NOTIFICATION_TYPE.SUCCESS)
        this.newTargetTableName = null
        this.crud.refresh()
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
